

let THREE = require('../../libs/three');

cc.Class({
    extends: cc.Component,

    properties: {
        sp_dst:cc.Sprite,

        textureRender: {
            default: null,
            type: cc.Texture2D
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.test();
    },

    test(){
        var scene = new THREE.Scene();
        scene.background = new THREE.Color( 0x666666 );
        var camera = new THREE.PerspectiveCamera( 75, this.node.width / this.node.height, 0.1, 1000 );
        camera.position.set( 40, 30, 40 ); // ( 0, -10, 40 )
        camera.lookAt( 0, 0, 0 );

        let light = new THREE.DirectionalLight(0xffffff,0.8);
        light.position.copy(camera.position);
        scene.add(light);

        var renderer = new THREE.WebGLRenderer( cc.game.canvas );
        renderer.setSize( this.node.width, this.node.height );

        // grid
        // var gridHelper = new THREE.GridHelper( 100, 20 );
        // gridHelper.rotation.x = Math.PI/2;
        // scene.add( gridHelper );

        // plane
        let planeGeo = new THREE.PlaneBufferGeometry(40,40,5);
        let planeMaterial = new THREE.MeshLambertMaterial({color:0x00ff00});
        let plane = new THREE.Mesh(planeGeo,planeMaterial);
        plane.rotation.x = -Math.PI/2;
        scene.add(plane);

        let axesHelper = new THREE.AxesHelper(100);
        scene.add(axesHelper);

        // cube
        var boxGeometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
        var cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff00 } );
        var cube = new THREE.Mesh( boxGeometry, cubeMaterial );
        cube.position.set( 0, 0, 0 );
        scene.add( cube );

        var ref = this;
        var animate = function () {

            requestAnimationFrame( animate );

            ref.textureRender.initWithElement(renderer.domElement);
            ref.sp_dst.spriteFrame.setTexture(ref.textureRender);

            // if (ref.state === 1) {
            // 	var x = cube.position.x + ref.velocity.x * 0.0025;
            // 	var y = cube.position.y + ref.velocity.y * 0.0025;
            // 	cube.position.set( x, y, 0 );
            // 	cube.rotation.set( 0, 0, ref.radian );
            //
            // 	camera.position.set( x, y, 40 );
            // }

            renderer.render( scene, camera );
        };

        animate();
    },

    // update (dt) {},
});
